/*
 * VisualNode.h
 *
 *  Created on: Apr 28, 2011
 *      Author: Matthew Michelotti
 */

#pragma once

#include <stdlib.h>
#include <iostream>

#include "Color.h"

class VisualNode {

public:
	inline VisualNode() :
		x(getRandPos()), y(getRandPos()), vx(0.0), vy(0.0), size(1) {

		color = Color(0.0f, 0.2f, 0.8f);
	}

	inline double getX() {
		return x;
	}
	inline double getY() {
		return y;
	}
	inline double getVX() {
		return vx;
	}
	inline double getVY() {
		return vy;
	}

	inline double getPxX() {
		return x * screenWidth;
	}
	inline double getPxY() {
		return y * screenHeight;
	}
	inline int getSize() {
		return size;
	}
	inline Color getColor() const {
		return color;
	}

	inline void setColor(const Color nodeColor) {
		color = nodeColor;
	}

	inline void setPos(double x, double y) {
		this->x = x;
		this->y = y;
		this->vx = 0.0;
		this->vy = 0.0;
	}

	inline void setSize(int size) {
		this->size = size;
	}

	inline void stepForce(double accelX, double accelY) {
		vx = bound(vx + accelX, maxSpeed);
		vy = bound(vy + accelY, maxSpeed);
		double oldX = x;
		double oldY = y;
		x = bound(x + vx, indent, 1.0 - indent);
		y = bound(y + vy, indent, 1.0 - indent);
		vx = x - oldX;
		vy = y - oldY;
	}

	inline void addNoise() {
		this->x += .03 * (.5 - (rand() % 0x1000) / (double) 0x1000);
		this->y += .03 * (.5 - (rand() % 0x1000) / (double) 0x1000);
	}

	static const int screenWidth;
	static const int screenHeight;

private:
	inline static double getRandPos() {
		return randDouble() * (1.0 - 2 * indent) + indent;
	}

	inline static double randDouble() {
		return (rand() % 0x1000) / (double) 0x1000;
	}

	inline static double bound(double value, double maxMag) {
		return bound(value, -maxMag, maxMag);
	}
	inline static double bound(double value, double min, double max) {
		if (value > max)
			return max;
		else if (value < min)
			return min;
		else
			return value;
	}

	double x;
	double y;
	double vx;
	double vy;
	int size;
	Color color;

	static const double indent;
	static const double maxSpeed;
};
